package com.jd.dlc.limit;

import com.alibaba.fastjson.JSONObject;
import com.jd.dlc.limit.biz.LimiterBencher;
import com.jd.dlc.limit.scan.Sanner;
import com.jd.neptune.painter.annotations.UMP;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;

/**
 * @program: dlc
 * @ClassName Trigger
 * @description:
 * @author: sizegang
 * @create: 2022-06-11
 **/
@Slf4j
@Component
public class Trigger {

    @UMP
    public void triggerLimiter(String namespace, String limitKey, ProceedingJoinPoint point, HttpServletResponse response, LimiterBencher bencher) {
        if (bencher != null) {
            log.error("### 触发限流 namespace{} limitKey {} bencher{}", namespace, limitKey, JSONObject.toJSONString(bencher));
        } else {
            log.error("### 触发限流 namespace{} limitKey {} ", namespace, limitKey);
        }
        Sanner.getBackers().get(namespace).triggerBacker(response, limitKey, point);
    }

    private void triggerLimiter(String namespace, String limitKey, ProceedingJoinPoint point, HttpServletResponse response) {
        triggerLimiter(namespace, limitKey, point, null, null);
    }


}
